package com.common.controller;

import com.common.aop.LoggerManage;
import com.user.domain.User;
import com.user.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.WebAuthenticationDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.security.Principal;
import java.util.UUID;

import static com.common.constant.RoleConstant.ROLE_USER;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * Created by sola on 2017/9/27.
 */
@Controller
public class LoginController {

    private UserRepository userRepository;

    private BCryptPasswordEncoder passwordEncoder;

    private StringRedisTemplate stringRedisTemplate;

    private RedisTemplate redisTemplate;

    @Autowired
    public LoginController(UserRepository userRepository, BCryptPasswordEncoder passwordEncoder, StringRedisTemplate stringRedisTemplate, RedisTemplate redisTemplate) {
        this.userRepository = userRepository;
        this.passwordEncoder = passwordEncoder;
        this.stringRedisTemplate = stringRedisTemplate;
        this.redisTemplate = redisTemplate;
    }

    @GetMapping("/")
    public String index(){
        return "index";
    }
    /**
     *登录操作
     **/
    @GetMapping(value = "/login")
    public String login(){
        return "login";
    }

    @GetMapping(value = "/register")
    public String toRegister(){

        return "register";
    }

    @RequestMapping(value = "/register", method = {POST})
    public String register(User user){
        user.setId(UUID.randomUUID().toString());
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        user.setRoles(ROLE_USER);
        if(null!=userRepository.findUserByUserName(user.getUserName())){
            return "redirect:register?error";
        }
        try {
            if (null != userRepository.save(user)) {
                return "redirect:register?success";
            }
            return "redirect:register?error";
        }catch (Exception e){
            return "redirect:register?error";
        }
    }

    @GetMapping("/success")
    @LoggerManage(description="success")
    public String user(@AuthenticationPrincipal Principal principal, Model model){
        model.addAttribute("username", principal.getName());
        WebAuthenticationDetails details = (WebAuthenticationDetails)SecurityContextHolder.getContext().getAuthentication().getDetails();
        String sessionId=details.getSessionId();
        stringRedisTemplate.opsForValue().set("stringTest","asdfg");
        redisTemplate.opsForValue().set("stringTest1","asdfg");
        String stringTest =stringRedisTemplate.opsForValue().get("stringTest");
        String stringTest1 =redisTemplate.opsForValue().get("stringTest1").toString();
        String stringTest1_ =(String)redisTemplate.opsForValue().get("stringTest1");
        stringRedisTemplate.opsForValue().set(sessionId,principal.toString());
        redisTemplate.opsForValue().set(sessionId+"1",principal);
        String string =stringRedisTemplate.opsForValue().get(sessionId);
        String webAuthenticationDetails =stringRedisTemplate.opsForValue().get(sessionId+"1");
        return "index";

    }

}
